package com.hs.sfzy.controller;

import com.hs.sfzy.util.Result;
import org.apache.commons.io.IOUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * @ClassName FileController
 * @Description TODO
 * @Author shanglin
 * @Date 2024年10月23日 15:32
 * @Version 1.0
 */
//@RestController
public class FileController_old {
    private String realPath = "\\\\SHANGLIN\\upLoad";

//    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return Result.error("未上传图片");
        }
        String filename = file.getOriginalFilename();
        filename = UUID.randomUUID().toString() + filename;
        // 上传
        file.transferTo(new File(realPath, filename));
        return Result.success(filename);
    }

    // 预览
//    @RequestMapping("/downLoad")
    public void downLoad(String filename, HttpServletResponse response) throws IOException {
        // 获取文件所属的真实路径
        File file = new File(realPath, filename);
        // 组成字节输出流
        FileInputStream fin = new FileInputStream(file);
        // 设置响应头信息
        response.setHeader("Content-Disposition", "filename=" + filename);//预览
        // 组成相应流
        ServletOutputStream out = response.getOutputStream();
        // 拷贝
        IOUtils.copy(fin, out);
        // 释放资源
        out.close();
        fin.close();
    }
}
